package com.fau.ss.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.ss.action.PublicAction;
import com.fau.ss.entity.MedicalAdmin;
import com.fau.ss.entity.MedicalSsPurchase;
import com.fau.ss.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 购买总金额统计Action
 * @author liy
 * @datetime 2014年11月3日,下午3:30:51
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class PurchaseTotalAmountStaAction extends PublicAction implements ModelDriven<MedicalSsPurchase>{

	private static final long serialVersionUID = -4743165361566354326L;
	
	private MedicalSsPurchase purchase = new MedicalSsPurchase();

	/**
	 * @desc 进入购买总金额统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年10月30日,下午9:54:13
	 */
	@Action(value = "amount_init", results = {@Result(name = SUCCESS, location = "amount_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入基本信息页面
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月10日,上午10:09:29
	 */
	@Action(value = "amount_basic", results = {@Result(name = SUCCESS, location = "amount_basic.jsp")})
	public String basic() {
		return SUCCESS;
	}
	
	/**
	 * @desc 基本统计分页查询
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,下午12:57:49
	 */
	@Action(value = "amount_basic_view")
	public String basicView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getPurchaseTotalAmountStaService().findBasicStaByPage(purchase, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 进入药品类别统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午1:43:56
	 */
	@Action(value = "amount_medicine_init", results = {@Result(name = SUCCESS, location = "amount_medicine_init.jsp")})
	public String medicine(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询药品类别集合
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,下午9:05:07
	 */
	@Action(value = "amount_find_medicine_sort")
	public String findMedicineSortList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchaseTotalAmountStaService().findMedicineSortList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 按药品类别分页查询
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,下午5:26:38
	 */
	@Action(value = "amount_medicine_view")
	public String medicineView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getPurchaseTotalAmountStaService().findMedicineStaByPage(purchase, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	
	/**
	 * @desc 进入年龄段统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,上午11:37:17
	 */
	@Action(value = "amount_age", results = {@Result(name = SUCCESS, location = "amount_age.jsp")})
	public String age(){
		return SUCCESS;
	}
	
	/**
	 * @desc 以年龄段统计
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,下午8:08:13
	 */
	@Action(value = "amount_age_view")
	public String ageView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchaseTotalAmountStaService().StatisticsByAgeRange(admin, purchase);
		formatJSON(response, list);
		return null;
	}
	
	
	/**
	 * @desc 进入年度统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,上午11:44:11
	 */
	@Action(value = "amount_year", results = {@Result(name = SUCCESS, location = "amount_year.jsp")})
	public String year(){
		return SUCCESS;
	}
	
	/**
	 * @desc 以年份统计
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,下午8:16:01
	 */
	@Action(value = "amount_year_view")
	public String yearView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchaseTotalAmountStaService().StatisticsByYear(admin, purchase);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入月份统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午1:36:25
	 */
	@Action(value = "amount_month", results = {@Result(name = SUCCESS, location = "amount_month.jsp")})
	public String month(){
		return SUCCESS;
	}
	
	/**
	 * @desc 以月份统计数据
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,下午8:59:01
	 */
	@Action(value = "amount_month_view")
	public String monthView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchaseTotalAmountStaService().StatisticsBymonth(admin, purchase);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入会员等级统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午2:10:20
	 */
	@Action(value = "amount_level", results = {@Result(name = SUCCESS, location = "amount_level.jsp")})
	public String level(){
		return SUCCESS;
	}
	
	/**
	 * @desc 以会员等级统计
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,下午8:26:16
	 */
	@Action(value = "amount_level_view")
	public String levelView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = this.baseAllService.getPurchaseTotalAmountStaService().StatisticsByLevel(admin, purchase);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入宣传区域统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午2:32:06
	 */
	@Action(value = "amount_area", results = {@Result(name = SUCCESS, location = "amount_area.jsp")})
	public String area(){
		return SUCCESS;
	}
	
	/**
	 * @desc 以宣传地区统计
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月19日,下午9:32:28
	 */
	@Action(value = "amount_area_view")
	public String areaView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = this.baseAllService.getPurchaseTotalAmountStaService().StatisticsByArea(admin, purchase);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量分配到话务员
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月20日,上午11:08:01
	 */
	@Action(value = "amount_allocate")
	public String allocate() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(purchase.getPhoneNum()) && StringUtils.isNotBlank(purchase.getUserId())) {
			this.baseAllService.getPurchaseTotalAmountStaService().allocate(purchase.getPhoneNum(), purchase.getUserId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 购买总金额基本统计导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月31日,上午12:38:52
	 */
	@Action(value = "amount_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"购买总金额基本统计导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPurchaseTotalAmountStaService().amountExportBasicAll(this.purchase);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 购买总金额药品类别统计导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月31日,上午9:25:06
	 */
	@Action(value = "amount_ms_export_all")
	public String medicalSortAllExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"购买总金额药品类别统计导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPurchaseTotalAmountStaService().amountMedicalSortExportAll(this.purchase);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalSsPurchase getModel() {
		return purchase;
	}
}
